<?php

namespace common\behavior\CommonMessagePushing;


use common\model\EntityModel;
use common\model\UserModel;
use common\service\BehaviorService;
use common\service\VersionService;
use Webman\Stomp\Client;

/**
 * 延期单待确认 消息通知
 */
class TaskReviewActiveNodeBehavior
{
    /**
     * @param $params
     * @return void
     */
    public function run($params)
    {
        /***********  预处理开始  ************/
        $params = BehaviorService::getInstance()->dealParam(
            $params,
            [
                'task' => '',
                'version_check_list' => '',
                'version' => '',
                'created_by' => 'intval',
                'review_request' => '',
                'nodes' => '',
            ],
            [
                'task' => [],
                'version_check_list' => [],
                'version' => [],
                'created_by' => 0,
                'review_request' => [],
                'nodes' => [],
            ],
            'data',
            ['type' => 'task_review_active_node'],
        );
        /***********  行为判断  ************/
        if ($params === false) {
            return;
        }
        /***********  变量迁移  ************/
        list($task, $versionCheckList, $version, $createdBy, $reviewRequest, $nodes) = $params;
        if (empty($task) || empty($version) || empty($createdBy) || empty($reviewRequest)) {
            return;
        }
        /***********  预处理结束  ************/

        /***********  原逻辑迁移  ************/

        $reviewMan = 0;
        $nodeId = 0;
        foreach ($nodes as $node) {
            if ($node['is_pass'] = "no" && $node['is_reviewed'] == "no" && $node['is_active'] == "yes") {
                $reviewMan = $node['plan_reviewer'];
                $nodeId = $node['id'];
                break;
            }
        }
        $creator = model(UserModel::class)->find($createdBy);
        if ($reviewMan == 0) {
            return;
        }

        $versionContentStr = "";
        foreach ($versionCheckList as $versionCheck) {
            $versionContentStr .= $versionCheck['content'];
        }
        $entity = model(EntityModel::class)->field('name')->find($task['entity_id']);
        $messageParam = [
            'first' => '任务已提交',
            'task_name' => $entity['name'] . "-" . $task['name'],
            'review_check_list' => mb_substr($versionContentStr, 0, 100),
            'be_review_man' => $creator['name'],
            'create_at' => date("Y/m/d H:i"),
            'remark' => '请及时审核',
            'version_id' => $version['id'],
            'review_request_id' => $reviewRequest['id'],
            'task_id' => $task['id'],
            'review_request_node_id' => $nodeId,
        ];
        $messageTemplateId = VersionService::getInstance()->getTaskReviewNotifyTemplateId();
        $reviewManData = model(UserModel::class)->field('union_id')->find($reviewMan);

        /***********  发送新的队列  ************/
        Client::send('common_message_pushing', build_queue_data_with_xu([
            'message_generated' => [[
                "param" => $messageParam,
                'receiver' => $reviewManData['union_id'],
                'template_id' => $messageTemplateId,
                'content_type' => 'card_message',
                'notice_type' => 'message_review',
                'is_popup' => 1,
                'message_key' => 'task_version_need_review',
            ]],
        ]));
        /***********  结束  ************/
    }
}
